package com.idreems.constellations;

import java.util.ArrayList;
import java.util.List;

import net.simonvt.menudrawer.MenuDrawer;
import net.simonvt.menudrawer.Position;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.idreems.SdkUtils;
import com.idreems.adapter.MenuAdapter;
import com.idreems.item.Category;
import com.idreems.item.Item;

public abstract class BaseListActivity extends FragmentActivity implements MenuAdapter.MenuListener {

    private static final String STATE_ACTIVE_POSITION =
            "net.simonvt.menudrawer.samples.LeftDrawerSample.activePosition";

    protected MenuDrawer mMenuDrawer;

    protected MenuAdapter mAdapter;
    protected ListView mList;

    private int mActivePosition = 0;

    private static final float kMenuDrqwerLeftMarginDip = 60;//dip
    @Override
    protected void onCreate(Bundle inState) {
        super.onCreate(inState);

        if (inState != null) {
            mActivePosition = inState.getInt(STATE_ACTIVE_POSITION);
        }

        mMenuDrawer = MenuDrawer.attach(this, MenuDrawer.Type.BEHIND, getDrawerPosition(), getDragMode());
        int width = SdkUtils.getScreenSize(this).x - SdkUtils.dip2px(getApplicationContext(), kMenuDrqwerLeftMarginDip);
        mMenuDrawer.setMenuSize(width);
        
        List<Object> items = new ArrayList<Object>();
        items.add(new Item("水瓶座[1/21-2/19]","shuiping", R.drawable.ic_action_refresh_dark));
        items.add(new Item("双鱼座[2/20-3/20]","shuangyu", R.drawable.ic_action_refresh_dark));
        items.add(new Item("白羊座[3/21-4/20]","baiyang", R.drawable.ic_action_refresh_dark));
        items.add(new Item("金牛座[4/21-5/21]","jinniu", R.drawable.ic_action_refresh_dark));
        items.add(new Item("双子座[5/22-6/21]","shuangzi", R.drawable.ic_action_refresh_dark));
        items.add(new Item("巨蟹座[6/22-7/23]","juxie", R.drawable.ic_action_refresh_dark));
        
        items.add(new Item("狮子座[7/24-8/23]","shizi", R.drawable.ic_action_refresh_dark));
        items.add(new Item("处女座[8/24-9/23]","chunv", R.drawable.ic_action_refresh_dark));
        items.add(new Item("天秤座[9/24-10/23]","tiancheng", R.drawable.ic_action_refresh_dark));
        items.add(new Item("天蝎座[10/24-11/22]","tianxie", R.drawable.ic_action_refresh_dark));
        items.add(new Item("射手座[11/23-12/22]","sheshou", R.drawable.ic_action_refresh_dark));
        items.add(new Item("摩羯座[12/23-1/22]","mojie", R.drawable.ic_action_refresh_dark));
        items.add(new Category("十二星座"));
        
        items.add(new Item("血型物语","xuexingwuyu", R.drawable.ic_action_refresh_dark));
        items.add(new Item("十二生肖","shiershengxiao", R.drawable.ic_action_refresh_dark));
        items.add(new Item("佛学禅语","foxuechanyu", R.drawable.ic_action_refresh_dark));
        items.add(new Item("腾讯星座","tencentxingzuo", R.drawable.ic_action_refresh_dark));
        items.add(new Item("风水玄学","fengshuixuanxue", R.drawable.ic_action_refresh_dark));
        items.add(new Item("测试控","ceshikong", R.drawable.ic_action_refresh_dark));
        items.add(new Item("星座运程","xingzuoyuncheng", R.drawable.ic_action_refresh_dark));
        items.add(new Category("星座物语"));

        mList = new ListView(this);

        mAdapter = new MenuAdapter(this, items);
        mAdapter.setListener(this);
        mAdapter.setActivePosition(mActivePosition);

        mList.setAdapter(mAdapter);
        mList.setOnItemClickListener(mItemClickListener);

        mMenuDrawer.setMenuView(mList);
    }

    protected abstract void onMenuItemClicked(int position, Item item);

    protected abstract int getDragMode();

    protected abstract Position getDrawerPosition();

    private AdapterView.OnItemClickListener mItemClickListener = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            mActivePosition = position;
            mMenuDrawer.setActiveView(view, position);
            mAdapter.setActivePosition(position);
            onMenuItemClicked(position, (Item) mAdapter.getItem(position));
        }
    };

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt(STATE_ACTIVE_POSITION, mActivePosition);
    }

    @Override
    public void onActiveViewChanged(View v) {
        mMenuDrawer.setActiveView(v, mActivePosition);
    }
    
}